# Contributor: Timo Teräs <timo.teras@iki.fi>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

pkgname=edk2
pkgver=0.0.201908
_realver=edk2-stable201908
_sslver=1.1.1b
_sfver=3e
pkgrel=1
pkgdesc="EFI Development Kit II"
url="https://github.com/tianocore/tianocore.github.io/wiki/EDK-II/"
arch="x86_64 aarch64"
license="BSD-2-Clause-Patent"
makedepends="bash python3 iasl nasm util-linux-dev"
options="!check" # has no checks
subpackages="ovmf::noarch"
source="$pkgname-$pkgver.tar.gz::https://github.com/tianocore/edk2/archive/$_realver.tar.gz
	https://www.openssl.org/source/openssl-$_sslver.tar.gz
	http://www.jhauser.us/arithmetic/SoftFloat-$_sfver.zip
	build-hack.patch
	disable-werror.patch
	"
builddir="$srcdir/$pkgname-$_realver"

case "$CARCH" in
x86)	 TARGET_ARCH=IA32;	PLATFORM=OvmfPkg/OvmfPkgIa32X64.dsc;;
x86_64)	 TARGET_ARCH=X64;	PLATFORM=OvmfPkg/OvmfPkgX64.dsc;;
aarch64) TARGET_ARCH=AARCH64;	PLATFORM=ArmVirtPkg/ArmVirtQemu.dsc;;
esac

TOOLCHAIN=GCC5
RELEASE=RELEASE

prepare() {
	# unix line endings for the files to be patched
	sed -e 's/\r$//' -i BaseTools/Source/C/VfrCompile/VfrUtilityLib.cpp \
		BaseTools/Source/C/VolInfo/VolInfo.c
	rm -rf CryptoPkg/Library/OpensslLib/openssl
	ln -s "$srcdir"/openssl-$_sslver CryptoPkg/Library/OpensslLib/openssl
	rm -rf ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3
	ln -s "$srcdir"/SoftFloat-$_sfver \
		ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3
	default_prepare
}

build() {
	export PYTHON_COMMAND=python3
	export WORKSPACE=$PWD
	export PACKAGES_PATH=$PWD
	export EDK_TOOLS_PATH=$PWD/BaseTools/
	export PATH=$PWD/BaseTools/BinWrappers/PosixLike/:$PATH

	bash -c ". edksetup.sh"
	make -C BaseTools

	msg "Building OVMF"
	command build -b ${RELEASE} -a ${TARGET_ARCH} -t ${TOOLCHAIN} -p ${PLATFORM} -n ${JOBS:-2}
}

package() {
	mkdir -p "$pkgdir"/usr/bin \
		"$pkgdir"/usr/share/$pkgname/Conf \
		"$pkgdir"/usr/share/$pkgname/Scripts

	install BaseTools/Source/C/bin/* BaseTools/BinWrappers/PosixLike/LzmaF86Compress \
		"$pkgdir"/usr/bin
	install BaseTools/BuildEnv "$pkgdir"/usr/share/$pkgname/
	install BaseTools/Conf/*.template "$pkgdir"/usr/share/$pkgname/Conf
	install BaseTools/Scripts/GccBase.lds "$pkgdir"/usr/share/$pkgname/Scripts

	for i in $(find BaseTools/Source/Python -type d -maxdepth 1); do
		local mod=${i##*/}
		test -f "$i/$mod.py" || continue
		cp -R BaseTools/Source/Python/"$mod" "$pkgdir"/usr/share/edk2/Python/
		cat <<- EOF > "$pkgdir"/usr/bin/"$mod".py
		#!/bin/sh
		export PYTHONPATH=/usr/share/edk2/Python
		exec $PYTHON_COMMAND /usr/share/edk2/Python/$mod/$mod.py "\$@"
		EOF
		chmod +x "$pkgdir"/usr/bin/"$mod".py
	done
}

ovmf() {
	pkgdesc="Open Virtual Machine Firmware (OVMF) BIOS"
	license="BSD MIT"

	for fw in "$builddir"/Build/*/${RELEASE}_${TOOLCHAIN}/FV/*.fd; do
		install -D $fw "$subpkgdir"/usr/share/OVMF/${fw##*/}
	done

	# dont ship memfd for now to save space
	rm -f "$subpkgdir"/usr/share/OVMF/MEMFD.fd

	# compat symlink
	case $CARCH in
		x86_64) local bios=OVMF.fd;;
		aarch64) local bios=QEMU_EFI.fd;;
	esac
	install -d "$subpkgdir"/usr/share/ovmf
	ln -sf ../OVMF/$bios "$subpkgdir"/usr/share/ovmf/bios.bin
}

sha512sums="439ed3d1ba0cfe7db4aa1a0b1298a4906e289bcb5600f9c248a3fcd9b9894b5591062c8f720ca432513a27e56ef0b858d15e33b38602258b09cecd5d2f9959a4  edk2-0.0.201908.tar.gz
b54025fbb4fe264466f3b0d762aad4be45bd23cd48bdb26d901d4c41a40bfd776177e02230995ab181a695435039dbad313f4b9a563239a70807a2e19ecf045d  openssl-1.1.1b.tar.gz
3fedcd0060affb2d8fc7995894133cfed6a495c8717df0d30c89885223c38749f25743598383736036332dad6353c6a3f027f5a94a696660f7c4b607e33e534c  SoftFloat-3e.zip
a7d4ab2c82b62ba01c86e59f53bd3896d661c9bfbb9db9598734155b66d5fe03eca4a2a9993a14d3bf555992c6d01ba5d7a15868ff9ec6ed98b8a9b3895bb7df  build-hack.patch
a7c566f2347ee08ee062be45c340f38dc047c31b1541b8eca994692c8d04f48fea45830dce5933699e9bf9207deeaf22e09639c605a896171f01d9bcd68878ce  disable-werror.patch"
